//run this script after jQuery loads, but before jQuery Mobile loads

//customize jQuery Mobile to let IE7+ in (Mobile IE)                
$(document).bind("mobileinit", function () {
    $.extend($.mobile, {

        //extend gradeA qualifier to include IE7+
        gradeA: function () {
            //IE version check by James Padolsey, modified by jdalton - from http://gist.github.com/527683
            var ie = (function () {
                var v = 3, div = document.createElement('div'), a = div.all || [];
                while (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><br><![endif]-->', a[0]);
                return v > 4 ? v : !v;
            } ());

            //must either support media queries or be IE7+
            return $.support.mediaquery || (ie && ie >= 7);
        },
        pageLoadErrorMessage: "",
        loadingMessage: false
    });
});

$(function () {
    $('body').bind('swipeleft', function(e) {
        if ($('.ui-page-active .next:visible').length > 0) {
        	$next = $('.ui-page-active .next');
            $.mobile.changePage($next.attr('href'), { transition: $next.attr('data-transition') });
        }
        e.stopImmediatePropagation();
        return false;
    });
    $('body').bind('swiperight', function(e) {
        if ($('.ui-page-active .prev:visible').length > 0) 
            $.mobile.changePage($('.ui-page-active .prev').attr('href'), { transition: 'slide reverse' });
        e.stopImmediatePropagation();
        return false;
    });
    $('body').show();
});

$('.ui-page').live('pageinit', function (event) {
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
        var viewportmeta = document.querySelector('meta[name="viewport"]');
        if (viewportmeta) {
            viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
            document.body.addEventListener('gesturestart', function () {
                viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
            }, false);
        }
    }
});

// Industry page specific events
$('#industry.ui-page-active').live('pageshow pagehide scrollstop', function (event) {
    var $demoPosition = $('#industry.ui-page-active .tiles .Row:eq(2)').position();
    $('#industry.ui-page-active .tiles .demo').css({ 'left': $demoPosition.left + 'px', 'top': $demoPosition.top + 70 + 'px' });
});

// pageshow and hide events
$('.ui-page').live('pageshow pagehide', function (event, ui) {
    if (event.type == 'pageshow') {
        addthis.toolbox('.addthis_toolbox');
        if ($(this).attr('data-role') == 'dialog' && ($(ui.prevPage).attr('id') == "industry" || $(ui.prevPage).attr('id') == "landing" || $(ui.prevPage).hasClass('demo'))) {
            $(ui.prevPage).fadeTo('fast', 0.1);
            var $demoPosition = $('#industry .tiles .Row:eq(2)').position();
            if ($demoPosition != null) {
                $('#industry .tiles .demo').css({ 'left': $demoPosition.left + 'px', 'top': $demoPosition.top + 70 + 'px' });
            }
        }
    }
    else if (event.type == 'pagehide') {
        if ($(ui.nextPage).attr('data-role') == 'page')
            $(ui.nextPage).removeAttr('style');
    }
});


$('.ui-page-active.ui-dialog').live('click', function(event) {
	var closeDialog = false;
    if (typeof(event.pageX) == 'number') {
        var $content = $(this).find('.ui-content');
        var $contentPos = $content.offset();
        if (event.pageX > $contentPos.left && event.pageX < $contentPos.left + $content.width() &&
            event.pageY > $contentPos.top && event.pageY < $contentPos.top + $content.height()) {
            if ($(event.target).hasClass('ui-btn-text')) {
                // email demo button click
                $.mobile.changePage('includes/emailDialog.php', {
                    role: 'dialog',
                    transition: 'pop'
                });
            }
            if ($('.ui-page-active .dialog-thanks:visible').length > 0)
            {
            	closeDialog = true;
            }
        }
        else {
        	closeDialog = true;
        }
        
        if (closeDialog) {
            $('.ui-dialog').each(function() {
                $(this).dialog('close');
                $('.dialog-thanks').hide();
                $('.dialog-form').show();
            });
        }
    }
});

$('.ui-page-active.ui-dialog #submit').live('click', function() {
    var formData = $("#callAjaxForm").serialize();

    $.ajax({
        type: "POST",
        url: "../email.php",
        cache: false,
        data: formData,
        success: onSuccess,
        error: onError
    });

    return false;
});

function onSuccess(data, status)
{
    $('.dialog-form').fadeOut(500).hide();
    $('.dialog-thanks').fadeIn(500).show();
    $('.ui-dialog .dialog-thanks .copy').vAlign();

}

function onError(data, status)
{
    // handle an error
}       

(function ($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);
